package org.yh.leetcode;

/**
 * @Author: yh
 * @Description:
 * @Date: Created in 10:17 2019/5/20
 * @Modified By:
 */
public class Ptrl {
    public static void main(String[] args) {
        int[] source = {1, 32, 12, 11, 32, 11, 32};
        Ptrl ptrl = new Ptrl();
        int[] del = ptrl.del(source, 1);
        if (del != null) {
            for (int i = 0; i < del.length; i++) {
                System.out.print(del[i] + " ");
            }
        }
    }


    /**
     * @param source 源数组
     * @param a      待删除元素
     * @return
     */
    public int[] del(int[] source, int a) {
        //新数组长度起始值为源数组长度
        int newLength = source.length;
        for (int i = 0; i < source.length; i++) {
            if (a == source[i]) {
                newLength--;
                continue;
            }
        }
        if (newLength == source.length) {
            System.out.println("该数组中不存在元素：" + a);
            return null;
        }
        int index = 0;
        int newArr[] = new int[newLength];
        for (int i = 0; i < source.length; i++) {
            if (a != source[i]) {
                newArr[index++] = source[i];
            }
        }
        return newArr;
    }
}
